package com.amigo.online.provider.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.provider.manager.mapper.AdvHomeApplyAreaTblMapper;
import com.amigo.online.provider.manager.mapper.AdvHomeApplyTblMapper;
import com.amigo.online.provider.manager.mapper.AdvHomeStockTblMapper;
import com.amigo.online.provider.manager.model.entity.AdvHomeApplyAreaTbl;
import com.amigo.online.provider.manager.model.entity.AdvHomeApplyTbl;
import com.amigo.online.provider.manager.model.entity.AdvHomeStockTbl;
import com.amigo.online.provider.manager.service.AdvHomeApplyTblService;
import com.amigo.online.provider.manager.util.OrderUtils;

@Service
public class AdvHomeApplyTblServiceImpl extends BaseService<AdvHomeApplyTbl> implements AdvHomeApplyTblService {


    @Resource
    private AdvHomeApplyTblMapper advHomeApplyTblMapper;

    @Resource
    private AdvHomeApplyAreaTblMapper advHomeApplyAreaTblMapper;

    @Resource
    private AdvHomeStockTblMapper advHomeStockTblMapper;

    @Override
    public List<AdvHomeApplyTbl> findAdvHomeApplyTbl(AdvHomeApplyTbl ahat) {

        return advHomeApplyTblMapper.findAdvHomeApplyTbl(ahat);
    }

    @Override
    public AdvHomeApplyTbl saveAdvHomeApplyTbl(AdvHomeApplyTbl ahat) {
        String orderId = OrderUtils.createOrderNumber();
        ahat.setOrderId(orderId);
        ahat.setStatus(0);
        ahat.setCreatedTime(System.currentTimeMillis() / 1000);
        if (advHomeApplyTblMapper.saveAdvHomeApplyTbl(ahat) <= 0) {
            return null;
        } else {
            List<Long> areaId = ahat.getAreaIds();
            Long advId = advHomeApplyTblMapper.findAdvHomeApplyById(ahat.getOrderId());
            if (areaId != null && areaId.size() > 0) {
                List<AdvHomeApplyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvHomeApplyAreaTbl e = new AdvHomeApplyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    e.setCreatedTime(System.currentTimeMillis() / 1000);
                    list.add(e);

                }
                if (advHomeApplyAreaTblMapper.insertList(list) > 0) {
                    return advHomeApplyTblMapper.selectByOrderId(orderId);
                }
            } else {
                return null;
            }
        }
        return null;

    }

    @Override
    public boolean updateAdvHomeApplyTbl(AdvHomeApplyTbl ahat) {
        ahat.setUpdatedTime(System.currentTimeMillis() / 1000);
        AdvHomeApplyTbl selectById = advHomeApplyTblMapper.selectById(ahat.getId());
        if (selectById.getStatus() == null || selectById.getStatus().equals(1)) {
            return false;
        }
        if (advHomeApplyTblMapper.updateAdvHomeApplyTbl(ahat) > 0) {
            List<Long> areaId = ahat.getAreaIds();// 获取地址id
            if (areaId != null && areaId.size() > 0) {
                Long advId = ahat.getId();// 申请表id
                AdvHomeApplyAreaTbl ex = new AdvHomeApplyAreaTbl();
                ex.setAdvId(advId);// 申请表id
                ex.setDeleted(1);
                ex.setDeletedTime(System.currentTimeMillis() / 1000);
                advHomeApplyAreaTblMapper.deleteAdvHomeApplyAreaTbl(ex);// 删除地址表的信息
                List<AdvHomeApplyAreaTbl> list = new ArrayList<>();
                for (Long area : areaId) {
                    AdvHomeApplyAreaTbl e = new AdvHomeApplyAreaTbl();
                    e.setAdvId(advId);
                    e.setAreaId(area);
                    list.add(e);
                }
                if (advHomeApplyAreaTblMapper.insertList(list) > 0) {
                    return true;
                }
            }
        } else {
            return false;
        }
        return false;
    }

    @Override
    public boolean deleteAdvHomeApplyTbl(AdvHomeApplyTbl ahat) {

        ahat.setDeletedTime(System.currentTimeMillis() / 1000);
        ahat.setDeleted(1);
        int i = advHomeApplyTblMapper.deleteAdvHomeApplyTbl(ahat);
        if (i <= 0) {
            return false;
        } else {

            return true;
        }
    }


    @Override
    public boolean checkAdv(AdvHomeApplyTbl ahat) {
        Long id = ahat.getId();
        advHomeApplyTblMapper.updateAdvHomeApplyTbl(ahat);
        if (ahat.getStatus() == 1) {
            AdvHomeApplyTbl advHomeApplyTbl = advHomeApplyTblMapper.selectById(id);
            AdvHomeStockTbl as = new AdvHomeStockTbl();
            as.setAdvId(advHomeApplyTbl.getId());
            as.setApplyNumber(advHomeApplyTbl.getTotalNumber());
            as.setRemainingNumber(advHomeApplyTbl.getTotalNumber());
            as.setOverUsedNumber(0);
            as.setUsedNumber(0);

            if (advHomeStockTblMapper.insert(as) > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

}
